package userInterface;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.text.DateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ACEmployee;
import model.Auction;
import model.Bidder;
import model.Control;
import model.NPOStaff;
import model.User;

/**
 * @author fahad
 * @author Daniel Bartholomew
 *
 */
@SuppressWarnings("serial")
public class AuctionView extends JFrame	{
	

	/** A date format used to create auction names */
	private static DateFormat my_format = DateFormat.getDateInstance(DateFormat.MEDIUM);
	
	/** Text field for auction view form to view	*/
	private JTextField auction_textfield;

	private JTextField NPO_textfield;

	private JTextField duration_textfield;

	private JTextField start_textfield;
	
	private JTextField date_textfield;

	private JButton btnBack = null;
	
	private static Auction my_auction;
	
	private Control my_control;
	
	/**
	 * Constructor to display a GUI for auction forms.
	 * Constructor
	 */
	public AuctionView(Auction the_auction, Control control)
	{
		super("Auction View");
		my_auction = the_auction;
		my_control = control;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		Auction(my_auction);
		setResizable(false);
		this.setSize(640, 480);

	}	
	
	public void Auction(Auction the_auction)
	{
		setLayout(new GridLayout(8,1));
		
		JPanel buttons = new JPanel();
		JButton selected_item_view = new JButton("View Items");
		selected_item_view.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				ItemListView ilv = new ItemListView(my_control, my_auction);
				ilv.setLocationRelativeTo(null);
				dispose();
			}
		});
		
		buttons.add(getBtnBack());
		buttons.add(selected_item_view);
		add(buttons);
		
		JPanel display_name = new JPanel();
		JLabel auction_name = new JLabel("Auction Name-----------------:");
		display_name.add(auction_name);
		auction_textfield = new JTextField(30);
		auction_textfield.setEditable(false);
		display_name.add(auction_textfield);
		auction_textfield.setText(the_auction.getName());
		add(display_name);
		
		JPanel NPO = new JPanel();
		JLabel NPO_Display = new JLabel("NPO----------------------------:");
		NPO.add(NPO_Display);
		NPO_textfield = new JTextField(30);
		NPO_textfield.setEditable(false);
		NPO.add(NPO_textfield);
		NPO_textfield.setText(the_auction.getNPO().getName());
		add(NPO);
		
		JPanel display_auction_date = new JPanel();
		JLabel auction_date = new JLabel("Auction Date---------------------:");
		display_auction_date.add(auction_date);	
		date_textfield = new JTextField(30);
		date_textfield.setEditable(false);
		display_auction_date.add(date_textfield);
		date_textfield.setText(my_format.format(the_auction.getDate().getTime()));
		add(display_auction_date);
	
		JPanel display_auction_start_time = new JPanel();
		JLabel start_auction = new JLabel("Auction Start Time--------------:");
		display_auction_start_time.add(start_auction);
		start_textfield = new JTextField(30);
		start_textfield.setEditable(false);
		display_auction_start_time.add(start_textfield);
		StringBuilder sb = new StringBuilder();
		sb.append(the_auction.getDate().get(Calendar.HOUR_OF_DAY));
		sb.append(":");
		int minutes = the_auction.getDate().get(Calendar.MINUTE);
		if (minutes < 10) {
			sb.append("0");
		}
		sb.append(minutes);
		start_textfield.setText(sb.toString());
		add(display_auction_start_time);

		JPanel display_auction_duration_time = new JPanel();
		JLabel auction_duration = new JLabel("Auction Duration(hours)-------:");
		display_auction_duration_time.add(auction_duration);		
		duration_textfield = new JTextField(30);
		duration_textfield.setEditable(false);
		display_auction_duration_time.add(duration_textfield);
		duration_textfield.setText(the_auction.getDuration() + "");
		add(display_auction_duration_time);
		
	}
	
	private JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton("Back");
			btnBack.setBounds(new Rectangle(495, 404, 106, 31));
			btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					User active = my_control.getActiveUser();
					if (active instanceof ACEmployee) {
						CalendarView cv = new CalendarView(my_control);
						cv.setLocationRelativeTo(null);
						dispose();
					} else if (active instanceof Bidder) {
						AuctionListView alv = new AuctionListView(my_control);
						alv.setLocationRelativeTo(null);
						dispose();
					}
				}
			});
		}
		if (my_control.getActiveUser() instanceof NPOStaff) {
			btnBack.setVisible(false);
		}
		return btnBack;
	}
}